package game

import (
	"context"
	"github.com/topfreegames/pitaya"
	"github.com/topfreegames/pitaya/component"
	"github.com/topfreegames/pitaya/fairy/constants"
	"github.com/topfreegames/pitaya/fairy/protos"
	"github.com/topfreegames/pitaya/logger"
)

type Logic struct {
	component.Base
}

func NewLogic() *Logic {
	return &Logic{}
}

/**
初始化游戏
 */
func (p *Logic) InitGame(ctx context.Context,req *protos.CGInitGame) (*protos.GCInitGameRet,error) {
	//todo:处理玩家离线存盘

	serverId := pitaya.GetServerID()
	playerAuthRet := &protos.WGPlayerAuthRet{}
	err := pitaya.RPC(ctx,constants.GWPlayerAuthRoute,playerAuthRet,&protos.GWPlayerAuth{
		AccountId: req.AccountId,
		PlayerId:  req.PlayerId,
		Ticket:    req.Ticket,
		AppId:     req.AppId,
		ServerId:  &serverId,
	})

	if err != nil {
		logger.Log.Errorf("InitGame failed,error=%s",err.Error())
		return nil,err
	}

	retCode := playerAuthRet.RetCode
	if *retCode !=  constants.InitGameAutuSuccess {
		return &protos.GCInitGameRet{
			RetCode:       retCode,
		},nil
	}

	//pDataMsg := playerAuthRet.PlayerData
	//playerId := pDataMsg.PlayerId
	return nil,nil
}